﻿using MediatR;
using SmartCity.ProcessEngine.Api.Read.Application.Command.FormCategory;
using SmartCity.ProcessEngine.Domain.Entities;
using SmartCity.ProcessEngine.ErrorCode;
using SmartCity.ProcessEngine.Infrastructure.Interface;

namespace SmartCity.ProcessEngine.Api.Read.Application.CommandHandler.FormCategory
{
    public class GetFormCategoryCodeHandler : IRequestHandler<GetFormCategoryCodeCommand, ApiResult<string>>
    {
        private readonly IBaseRepository<FormCategoryModel> categoryRepo;

        public GetFormCategoryCodeHandler(IBaseRepository<FormCategoryModel> categoryRepo)
        {
            this.categoryRepo = categoryRepo;
        }

        /// <summary>
        /// 获取当前的编号
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> Handle(GetFormCategoryCodeCommand request, CancellationToken cancellationToken)
        {
            var count = categoryRepo.GetAll().Count()+1;//不能过滤已逻辑删除

            string code = "T" + count.ToString().PadLeft(4, '0');

            return new ApiResult<string>().Success("成功获取编号",code);

        }
    }
}
